var express = require('express');
var router = express.Router();
const ListModel = require("../../models/ListModel")
const moment = require("moment")
const checkLoginMiddleware = require('../../middlewares/checkLoginMiddleware')

/* GET home page. */
router.get('/',function(req,res,next){
  res.redirect("/accounts")
})

router.get('/accounts',checkLoginMiddleware,function(req, res, next) {
  ListModel.find().sort({time:-1}).then(data=>{
    res.render('accounts',{accounts:data,moment:moment})
  }).
  catch(error=>{
    res.status(500).send('查找失败')
  })

  
});

router.get('/add-account',checkLoginMiddleware,function(req,res,next){
  res.render('addAccount')
})

router.post('/add-account',checkLoginMiddleware,function(req,res,next){
  ListModel.create({...req.body,time:new Date(req.body.time)})
  .catch(error=>{
    res.status(500).send('创建失败')
  })
  res.render('response',{url:'/accounts',msg:'添加成功了'})
})

router.get('/remove-account/:id',checkLoginMiddleware,function(req,res,next){
  console.log(req.params.id)
  ListModel.deleteOne({_id:req.params.id}).then(data=>{
    res.render('response',{url:'/accounts',msg:'删除成功了'})
  }).catch(error=>{
    res.status(500).send('删除失败')
  })
  
  
})

router.get('/zyc/da/mei/lv',function(req,res,next){
  res.render('zyc',{})
})

module.exports = router;
